﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace He.DAL
{
    public partial class DbHelperSQLite
    {
        public static void UpdateField(string tblName, int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("update {0} set {1} where id={2}", tblName, strValue, id);
            DbHelperSQLite.ExecuteSql(strSql.ToString());
        }

        public static DataSet GetList(string tblName, int num, string columnNames, string strWhere, string orderFiled)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from {1} ", columnNames, tblName);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.AppendFormat(" order by {0} limit {1}", orderFiled, num);
            return DbHelperSQLite.Query(strSql.ToString());
        }

        public static int GetRecordCount(string tblName, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select count(*) as H  from {0} ", tblName);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(GetSingle(strSql.ToString()));
        }

        public static DataSet GetPageList(string tblName, int pageSize, int currentPage, string columnNames, string strWhere, string orderFiled)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select {0} FROM {1}", columnNames, tblName);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            int skip = pageSize * currentPage;
            return Query(string.Format("select {0} from {1}  where id in(select id FROM {1} where {2} order by {3} limit {4},{5}) order by {3}", new object[] { columnNames, tblName, strWhere, orderFiled, skip, pageSize }));
        }
    }
}
